<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/X-admin/css/font.css">
    <link rel="stylesheet" href="/X-admin/css/login.css">
    <link rel="stylesheet" href="/X-admin/css/xadmin.css">
    <script src="/js/jquery.js"></script>
    <script src="/X-admin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/X-admin/js/xadmin.js"></script>

    <script src="../../js/myAjax.js"></script>

</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label">
                    <span class="x-red">*</span>用户名</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_username" name="username" lay-verify="username"
                           autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="L_pass" name="password" autocomplete="off"
                           class="layui-input"></div>
                <div class="layui-form-mid layui-word-aux">6到16个字符</div>
            </div>
            <div class="layui-form-item">
                <label for="repass" class="layui-form-label">
                    <span class="x-red">*</span>确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="repass" name="repass"
                           autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="phone" class="layui-form-label">
                    <span class="x-red">*</span>手机</label>
                <div class="layui-input-inline">
                    <input type="text" id="phone" name="phone"
                           autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>爱好</label>
                <div class="layui-input-inline">
                    <input type="checkbox" lay-filter="hobby" name="hobby" title="旅游" value="旅游">
                    <input type="checkbox" lay-filter="hobby" name="hobby" title="看书" value="看书">
                    <input type="checkbox" lay-filter="hobby" name="hobby" title="游戏" value="游戏">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="birthday" class="layui-form-label">
                    <span class="x-red">*</span>生日</label>
                <div class="layui-input-inline">
                    <input class="layui-input" autocomplete="off" placeholder="生日" name="birthday" id="birthday">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>性别</label>
                <div class="layui-input-inline">
                    <select name="sex" lay-search="" style="width: 80px">
                        <option value="">性别选择</option>
                        <option value="">全部</option>
                        <option value="男">男</option>
                        <option value="女">女</option>
                        <option value="保密">保密</option>
                        <!--                                配置文件-->
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <button class="layui-btn" lay-filter="add" lay-submit="">增加bbbbb</button>
            </div>
        </form>
    </div>
</div>
<script>layui.use(['laydate', 'form', 'layer', 'jquery'],
    function () {
        $ = layui.jquery;
        var laydate = layui.laydate;
        var form = layui.form,
            layer = layui.layer;
        //执行一个laydate实例
        laydate.render({
            elem: '#birthday' //指定元素
            , trigger: 'click'
        });
        let checkBoxArr = [];
        form.on('checkbox(hobby)', function (data) {
            console.log(data);

            //选中状态下改变按钮为可点击状态
            if (data.elem.checked) {
                // $('#del').removeClass('layui-btn-disabled');
                checkBoxArr.push(data.elem.value);

            } else if (!data.elem.checked) {
                //取消选中时检查是否还有其他复选框被选中，所有复选框都没选中状态则改变按钮为禁用状态
                $.each(checkBoxArr, function (i, a) {
                    if (a == data.elem.value) {
                        checkBoxArr.splice(i, 1);
                    }
                });
                if (checkBoxArr.length < 1) {
                    // $('#del').addClass('layui-btn-disabled');
                }
            }
            console.log(checkBoxArr);
            //console.log(data.elem); //得到checkbox原始DOM对象
            //console.log(data.elem.checked); //是否被选中，true或者false
            //console.log(data.value); //复选框value值，也可以通过data.elem.value得到
            //console.log(data.othis); //得到美化后的DOM对象
        });
        //自定义验证规则
        form.verify({
            username: [/^[A-z]\w{5,15}$/, "用户名必须6到16为，不能为特殊字符"],
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(add)',
            function (data) {
                data = data.field;
                data.hobby = checkBoxArr.toLocaleString();
                console.log(data);
                //发异步，把数据提交给java后台
                let result = myAjax("/back/user/add", data, "post");
                if (result.result == 'isHave') {
                    layer.alert("当前用户名以及存在");
                } else if (result.result == 'success') {
                    layer.alert("增加成功", {
                            icon: 6
                        },
                        function () {
                            //关闭当前frame
                            xadmin.close();

                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                } else {
                    layer.alert("增加失败");
                }
                return false;
            });

    });</script>
<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>